<?php

namespace App\Models;

use Illuminate\Support\Facades\DB;

class TagPost extends Model
{
    protected $table = 'tag_post';
    public      $timestamps = false;
    /**
     * @param $id
     * @return mixed
     * @name 通过id获取一条信息
     * @author Deann.wu
     */
    public static function getInfoById($id){
         return DB::table('tag_post')->where('id',$id)->first();
    }

    /**
     * @param $post_id
     * @return mixed
     * @name 通过文章id获取信息
     * @author Deann.wu
     */
    public static function getListByUserIdArray($post_id){
        return DB::table('tag_post')
            ->where('tag_post.post_id',$post_id)
            ->join('tags', 'tags.id', '=', 'tag_post.tag_id')
            ->pluck('tags.name')
            ->toArray();
    }

    /**
     * @param $post_id
     * @return mixed
     * @author Deann.wu
     * @name 通过id 数据类基本信息
     */
    public static function getListByUserIdArrayAll($post_id){
        return DB::table('tag_post')
            ->where('tag_post.post_id',$post_id)
            ->join('tags', 'tags.id', '=', 'tag_post.tag_id')
            ->select('tags.id','tags.name')
            ->get()
            ->toArray();
    }
    /**
     * @param $tag_id
     * @return mixed
     * @author Deann.wu
     * @name 通过标签id获取数量
     */
    public static function getCountByTagId($tag_id){
        return DB::table('tag_post')->where('tag_id',$tag_id)->count();
    }

    /**
     * @param $tag_id
     * @return mixed
     * @author Deann.wu
     * @name 获取指定标签下的所有商品
     */
    public static function getGoodsByTagsList($tag_id){
         $tp_lists=DB::table('tag_post')->where('tag_id',$tag_id)->paginate(15);
         foreach ($tp_lists as $list){
             //获取文章信息
             $ob_post=Post::getInfoById($list->post_id);
             $list->post_title=$ob_post->title;//名字
             $list->post_id=$ob_post->id;//id
             $list->post_created_at=$ob_post->created_at;//创建时间
             $list->post_b_count=$ob_post->b_count;//浏览量
         }
         return $tp_lists;
    }

}
